import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class tester {
    
    public static void main(String[] args) {
        final JavaBoard board = new JavaBoard(sJavaMap);
        final JavaOverlayBoard overlay = new JavaOverlayBoard(sJavaMap);
        HexTileBoardView boardView = new HexTileBoardView(board, overlay);
        
        board.addModelChangeListener(boardView);
        overlay.addModelChangeListener(boardView);
        
        Frame frame = new Frame(1008, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(boardView);
        frame.show();
        
        boardView.requestFocusInWindow();
        
        /*
         * This loop places tiles of random types on 40% of the board in random
         * locations. Comment out this block to see a blank board.
         * 
         * Notice the border color of the overlay tile indicates whether we can
         * place a tile or not (green if we can, red if we can't). Pressing enter
         * does nothing when the border is red, and places the tile when the border
         * is green.
         */
        for(int[] coords : sJavaMap) {
            if(Math.random() * 100 < 40) {
                board.placeTile(coords, new JavaTile((int) (Math.random() * 4)));
            }
        }
        
        /*
         * This KeyListener listens for input from the arrow pad to move a tile
         * on the overlay board. The HexTileBoardView draws two boards (the game
         * board and an overlay board) and listens to changes from either via
         * the ObservableModel.ModelChangeListener interface.
         * 
         * Press 'T' to prepare to place a new village tile.
         * Navigate with the arrow keys.
         * Press 'Enter' to place the tile.
         */
        boardView.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent event) {
                int direction = -1;
                switch(event.getKeyCode()) {
                    case KeyEvent.VK_T :
                        overlay.placeTile(new int[] { 0, 0 }, new JavaTile(JavaTile.TYPE_VILLAGE));
                        if(board.isValid(overlay.getTileCoordinates())) {
                            overlay.setValid(true);
                        } else {
                            overlay.setValid(false);
                        }
                        break;
                    case KeyEvent.VK_ENTER :
                        if(board.placeTile(overlay.getTileCoordinates(), overlay.getTile())) {
                            overlay.clear();
                        }
                        break;
                    case KeyEvent.VK_ESCAPE :
                        overlay.clear();
                        break;
                    case KeyEvent.VK_DOWN :
//                        System.out.println("down");
                        direction = JavaBoard.SOUTHEAST;
                        break;
                    case KeyEvent.VK_LEFT :
//                        System.out.println("left");
                        direction = JavaBoard.WEST;
                        break;
                    case KeyEvent.VK_RIGHT :
//                        System.out.println("right");
                        direction = JavaBoard.EAST;
                        break;
                    case KeyEvent.VK_UP :
//                        System.out.println("up");
                        direction = JavaBoard.NORTHWEST;
                        break;
                }
                
                if(direction != -1) {
                    overlay.moveTile(direction);
                    if(board.isValid(overlay.getTileCoordinates())) {
                        overlay.setValid(true);
                    } else {
                        overlay.setValid(false);
                    }
                }
            }
            public void keyReleased(KeyEvent arg0) {
            }
            public void keyTyped(KeyEvent arg0) {
            }
        });
    }

//	public static void main(String[] args) {
////		ActionTester test = new ActionTester();
//////		test.printXandY();
////		test.method1(2, 5);
////		
////		Action a = test.getTop();
////		System.out.println("a: " + a.Serialize());
////		
////		Action b = Action.fromString(a.Serialize());
////		System.out.println("b: " + b.Serialize());
////		
////		a.printArgs();
////		b.printArgs();
////		a.printRedo();
////		b.printRedo();
////		
////		test.printXandY();
////		a.RedoAction(test);
////		test.printXandY();
////		
////		test.printXandY();
////		b.RedoAction(test);
////		test.printXandY();
////		
////		
//////		test.printXandY();
//////		test.undoAction(true);
//////		test.printXandY();
//////		test.redoAction();
//////		test.printXandY();
//		
//		
//		
//		JFrame f = new JFrame();
//		f.setSize(500,500);
//		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		f.setVisible(true);
//		JPanel p = new JPanel();
//		
//		Cell c = new Cell();
//		CellPanel cp = new CellPanel();
//		c.addObserver(cp);
//		
//		p.add(cp);
//		f.add(p);
//		c.molestStats(5, "hey", 10);
//		
//		try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		c.molestStats(50, "changed", 69);
//		
////		GameFrame f = new GameFrame();
////		f.setVisible(true);
////		f.setSize(1300,800);
////		
////		GameState gs = new GameState();
////		gs.addInputKeyListener(KeyEvent.VK_S, new Runnable() {
////			public void run() {
////				System.out.println("you pressed S!");
////			}
////		}, "Press the S key");
////		gs.addInputKeyListener(KeyEvent.VK_D, new Runnable() {
////			public void run() {
////				System.out.println("you pressed D!");
////			}
////		}, "Press the D key");
////		
////		f.changeGameState(gs);
//		
//
//	}

    
    private static int[][] sJavaMap = new int[][] {
            {-9, 2},                                                                                                                // x = -9
            {-8, 0}, {-8, 1}, {-8, 2}, {-8, 3},                                                                                     // x = -8
            {-7, -1}, {-7, 0}, {-7, 1}, {-7, 2}, {-7, 3}, {-7, 4}, {-7, 5},                                                         // x = -7
            {-6, -1}, {-6, 0}, {-6, 1}, {-6, 2}, {-6, 3}, {-6, 4},                                                                  // x = -6
            {-5, -3}, {-5, -2}, {-5, -1}, {-5, 0}, {-5, 1}, {-5, 2}, {-5, 3},                                                       // x = -5
            {-4, -4}, {-4, -3}, {-4, -2}, {-4, -1}, {-4, 0}, {-4, 1}, {-4, 2}, {-4, 3}, {-4, 4}, {-4, 5},                           // x = -4
            {-3, -4}, {-3, -3}, {-3, -2}, {-3, -1}, {-3, 0}, {-3, 1}, {-3, 2}, {-3, 3}, {-3, 4},                                    // x = -3
            {-2, -5}, {-2, -4}, {-2, -3}, {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}, {-2, 3},                                   // x = -2
            {-1, -6}, {-1, -5}, {-1, -4}, {-1, -3}, {-1, -2}, {-1, -1}, {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3}, {-1, 4}, {-1, 5},       // x = -1
            {0, -7}, {0, -6}, {0, -5}, {0, -4}, {0, -3}, {0, -2}, {0, -1}, {0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4},                  // x =  0
            {1, -7}, {1, -6}, {1, -5}, {1, -4}, {1, -3}, {1, -2}, {1, -1}, {1, 0}, {1, 1}, {1, 2}, {1, 3},                          // x =  1
            {2, -5}, {2, -4}, {2, -3}, {2, -2}, {2, -1}, {2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5},                            // x =  2
            {3, -6}, {3, -5}, {3, -4}, {3, -3}, {3, -2}, {3, -1}, {3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4},                           // x =  3
            {4, -5}, {4, -4}, {4, -3}, {4, -2}, {4, -1}, {4, 0}, {4, 1}, {4, 2}, {4, 3},                                            // x =  4
            {5, 3}, {5, -6}, {5, -5}, {5, -4}, {5, -3}, {5, -2}, {5, -1}, {5, 0}, {5, 1}, {5, 2},                                   // x =  5
            {6, -7}, {6, -6}, {6, -5}, {6, -4}, {6, -3}, {6, -2}, {6, -1}, {6, 0}, {6, 1}, {6, 2},                                  // x =  6
            {7, -7}, {7, -6}, {7, -5}, {7, -4}, {7, -3}, {7, -2}, {7, -1}, {7, 0}, {7, 1}, {7, 2},                                  // x =  7
            {8, -4}, {8, -3}, {8, -1}, {8, 0}                                                                                       // x =  8
    };
}
